package com.powermgr.boringtest.utils;

import lombok.extern.log4j.Log4j2;

import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;

/**
 * ScriptUtils
 *
 * @author qiuxiangdong
 * @date 2023/6/12
 */
@Log4j2
public class CmdUtils {
    public static void runXTS(String prType, String xtsName, String sn){
        log.info("*开始执行XTS用例: " + prType + " == " + xtsName);
        String[] args = new String[0];
        if (TypeUtils.IsActs(Collections.singletonList(prType))) {
            args = new String[]{"cmd", "/c", ConfigUtils.SN + File.separator + sn +  File.separator + "case" +  File.separator
                    + "acts" + File.separator + "run.bat", "run", "-l", xtsName};
        }
        if (TypeUtils.IsHats(Collections.singletonList(prType))) {
            args = new String[]{"cmd", "/c", ConfigUtils.SN + File.separator + sn +  File.separator + "case" + File.separator
                    + "hats" + File.separator + "run.bat", "run", "-l", xtsName};
        }
        ProcessBuilder pb = new ProcessBuilder(args);
        try {
            Process process = pb.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = reader.readLine()) != null) {
                log.info(line);
            }
            process.waitFor();
            reader.close();
        } catch (IOException | InterruptedException e) {
            log.error("-----runXTS 失败", e);
        }
    }
    public static void runTDD(String prType, String prModule, String sn) {
        log.info("*开始执行TDD用例: " + prType + " == " + prModule);
        String[] args = new String[]{"cmd", "/c", ConfigUtils.SN + File.separator + sn +  File.separator + "case"
                + File.separator + "developertest" + File.separator + "start.bat",
                ConfigUtils.SN + File.separator + sn +  File.separator + "case"
                        + File.separator + "developertest"};
        ProcessBuilder pb = new ProcessBuilder(args);
        try {
            Process process = pb.start();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            writer.write("1" + "\n");
            //writer.write("run -t "+ prType + " -tp " + prModule + "\n");
            writer.write("run -t "+ prType + "\n");
            writer.write("quit\n");
            writer.write("exit(0)\n");
            writer.flush();
            String line = "";
            while ((line = reader.readLine()) != null) {
                log.info(line);
            }
            process.waitFor();
            reader.close();
            writer.close();
        } catch (IOException | InterruptedException e) {
            log.error("-----runUtFuzzBenchmark 失败", e);
        }
    }

    public static List<String> runCmd(String cmd) {
        String[] args = new String[]{"cmd", "/c", cmd};
        ProcessBuilder pb = new ProcessBuilder(args);
        String line = null;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            Process process = pb.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line).append(" ");
            }
            process.waitFor();
            reader.close();
            return Arrays.asList(stringBuilder.toString().split(" "));
        } catch (IOException | InterruptedException ignored) {
        }
        return null;
    }
}
